<?php

/***
*
* FILE: /classes/dbdrive.php
* DESC: Файл с основным синглтон-классом доступа к БД
* VER: 0.0.1
*
***/

// security check
if (!defined('NDIRECT')) die ('PLEASE DONT DIRECT');

// debug check
if (!defined('DEBUG')) define ('DEBUG', false);

class dbdrive {
    
    // singleton realization
    private static $instance;
    
    // db object state
    var $stateOfDB;
    
    // connection data
    var $dbConnConnection;
    
    // query data
    var $curQuery;
    var $curQueryResource;
    
    // parsed query data
    var $curQueryArray;
    
    function __clone() {
        trigger_error('Singleton cloning is deprecated', E_USER_ERROR);
    }
    
    function __wakeup() {
        trigger_error('Singleton deserialization is deprecated.', E_USER_ERROR);;
    }
    
    public function init ($host,$base,$user,$pass) {
        
        if (!isset(self::$instance)) {
            $className = __CLASS__;
            self::$instance = new $className($host,$base,$user,$pass);
        }
        
        return (self::$instance);
        
    } // init
    
    // constructor
    private function __construct($host,$base,$user,$pass) {
        
        if ((isset($host) && !is_null($host)) && (isset($base) && !is_null($base)) && (isset($user) && !is_null($user)) && (isset($pass) && !is_null($pass))) {
            
            if (!DEBUG) {
                
                $this->dbConnConnection = @mysql_connect($host, $user, $pass);
                if (!$this->dbConnConnection) {
                    $this->stateOfDB = false;
                    return;
                }

                $dbConnSelectDB = @mysql_select_db ($base,$this->dbConnConnection);
                if (!$dbConnSelectDB) {
                    $this->stateOfDB = false;
                    return;
                }
                
            } else {
                
                $this->dbConnConnection = mysql_connect($host, $user, $pass);
                if (!$this->dbConnConnection) {
                    $this->stateOfDB = false;
                    return;
                }

                $dbConnSelectDB = mysql_select_db ($base,$this->dbConnConnection);
                if (!$dbConnSelectDB) {
                    $this->stateOfDB = false;
                    return;
                }
                
            }
            
            $this->stateOfDB = true;
            return;
            
        } else {
            
            $this->stateOfDB = false;
            return;

        }
        
    } // __construct
    
    function prepQuery($queryString) {
        
        if (!$this->stateOfDB) return (false);
        
        // verify and sanitize query string
        // cache work
        $this->curQuery = $queryString;
        
    } // prepQuery
    
    function runQuery () {
        
        if (!$this->curQuery) return (false);
        if (!$this->stateOfDB) return (false);
        
        if (DEBUG) {
            $this->curQueryResource = @mysql_query($this->curQuery, $this->dbConnConnection);
        } else {
            $this->curQueryResource = mysql_query($this->curQuery, $this->dbConnConnection);
        }
        
        if ($this->curQueryResource === false) {
            return (false);
        } else {
            return (mysql_affected_rows($this->dbConnConnection));
        }
        
    } // runQuery
    
    function lastQueryAsAssocArray () {
        
        if (!$this->stateOfDB) return (false);
        
        $resArr = array();
        while($curDbRow=mysql_fetch_array($this->curQueryResource,MYSQL_ASSOC)) {
            $resArr[] = $curDbRow;
        }
        
        return ($resArr);
        
    } // lastQueryAsArray
    
    function lastQueryAsNumArray () {
        
        if (!$this->stateOfDB) return (false);
        
        $resArr = array();
        while($curDbRow=mysql_fetch_array($this->curQueryResource,MYSQL_NUM)) {
            $resArr[] = $curDbRow;
        }
        
        return ($resArr);
        
    } // lastQueryAsArray
    
}

?>
